package com.rdk.action;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rdk.constant.Forward;
import com.rdk.constant.StrConstant;
import com.rdk.entity.CheckRequstParam;
import com.rdk.entity.CheckResponseEntity;
import com.rdk.entity.KwsRequestParam;
import com.rdk.entity.KwsResult;
import com.rdk.entity.TShareAttachment;
import com.rdk.service.CheckUserService;
import com.rdk.service.PSKeyWordService;
import com.rdk.service.TShareAttachmentService;
import com.rdk.service.TUserService;
import com.rdk.service.impl.CheckUserServiceImpl;
import com.rdk.service.impl.PSKeyWordServiceImpl;
import com.rdk.utils.CommonUtil;

/**
 * 关注app
 * @author wang_lei
 *
 */
@Controller
public class Attention extends BaseAction {
	
	@Autowired
	private TShareAttachmentService attaService;
	
	@Autowired
    private TUserService userService;
	
	/**
	 * to首页
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "attention/index")
	public String toAttentionIndex(HttpServletRequest request, ModelMap map){
		
		String userToken = CommonUtil.getSession(request, StrConstant.PARAM_USERTONKEN);
		PSKeyWordService psKeyWord = new PSKeyWordServiceImpl();
		map.put(StrConstant.PARAM_USERTONKEN, userToken);
		// 设置userToken
		String firstGID = this.getFirstGID(userToken);
		
		KwsRequestParam kwsRequestParam = new KwsRequestParam();
		kwsRequestParam.setUserAgentNo(userToken);
		kwsRequestParam.setCurrentPage(1);
		// 获取关键字
		KwsResult kws = psKeyWord.getAllkws(kwsRequestParam, firstGID);
		Set<String> kwss = new HashSet<String>();

		if(kws != null && kws.getData() != null && kws.getData().size()>0 && kws.getData().get(0).getKwsmodel1() !=null && kws.getData().get(0).getKwsmodel1() !=null){
			for(String t : kws.getData().get(0).getKwsmodel1().split(",")){  
				kwss.add(t);  
			}
			CommonUtil.setSession(request, "kwss", kwss);
		}else{
			CommonUtil.setSession(request, "keyWordIsNull", StrConstant.KEYWORD_IS_NULL);
		}
		
		map.put("kwss", kwss);
		return Forward.ActionPage.ATTENTION_INDEX;
	}
	
	private String getFirstGID(String userToken){
		CheckRequstParam userParma = new CheckRequstParam();
		userParma.setUserAgentNo(userToken);
		
		CheckUserService checkUserService = new CheckUserServiceImpl();
		CheckResponseEntity ce = checkUserService.getUserAccount(userParma);
		String firstGID = null;
		if (null != ce){
			firstGID = ce.getFirstGID();
		}
		
		
		return firstGID;
	}
	
	/**
	 * 获取分享图片()
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "attention/getSharePhoto")
	public ModelAndView getSharePhoto(HttpServletRequest request, ModelMap map){
		
		
		//String userId = CommonUtil.getSession(request, StrConstant.USERID);
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		
		if(null == CommonUtil.getSessionObj(request, "friends")){
			CommonUtil.setSession(request, "friends", userService.findUserFriends(userId));
		}
		
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		
		Map<String, Object> shareMap = new HashMap<String,Object>();
		shareMap.put("userId", userId);
		
		List<TShareAttachment> list = attaService.selectByShareIdForPhoto(userId);
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		object.put("data", list);
		
		return new ModelAndView("jsonView", object);
	}
	
}
